package com.lfd.rabbitmq.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @项目名： spring-emq
 * @包名： com.lfd.rabbitmq.config
 * @文件名: DirectRabbitConfig
 * @创建者: liufudong@szyh-smart.com
 * @创建时间: 2024/10/11 21:09
 * @描述：
 */
@Configuration
@Slf4j
public class DirectRabbitConfig {
    /**
     * 1. 创建死信交换机和死信队列
     */
    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange("dead_letter_exchange");
    }

    @Bean
    public Queue deadLetterQueue() {
        return QueueBuilder.durable("dead_letter_queue").build();
    }

    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue())
                .to(deadLetterExchange())
                .with("dead_letter_routing_key");
    }

    /**
     * 2. 创建业务队列并绑定到死信队列，设置消息TTL
     */
    @Bean
    public Queue businessQueue() {
        return QueueBuilder.durable("business_queue")
                // 指定死信交换机
                .withArgument("x-dead-letter-exchange", "dead_letter_exchange")
                // 指定死信队列的路由键
                .withArgument("x-dead-letter-routing-key", "dead_letter_routing_key")
                // 设置消息的过期时间（60秒）
//                .withArgument("x-message-ttl", 60000)  // 60秒的TTL

                .withArgument("x-message-ttl",5000)  // 60秒的TTL
                .build();
    }

    @Bean
    public DirectExchange businessExchange() {
        return new DirectExchange("business_exchange");
    }

    @Bean
    public Binding businessBinding() {
        return BindingBuilder.bind(businessQueue())
                .to(businessExchange())
                .with("business_routing_key");
    }
}
